package com.example.homeworkmanagement.controller;


import com.example.homeworkmanagement.dto.ClassesAndStudentsDTO;
import com.example.homeworkmanagement.pojo.Students;
import com.example.homeworkmanagement.service.StudentsService;
import com.example.homeworkmanagement.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 拒绝加班
 * @since 2025-05-25
 */
@RestController
@RequestMapping("/students")
@Api(tags="学生信息管理",description="提供管理API接口")
@CrossOrigin
public class StudentsController {
    @Autowired
    private StudentsService studentsService;

    @GetMapping("/login")
    @ApiOperation("学生登录")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string",name = "student_id",value = "学生学号id",required = true),
            @ApiImplicitParam(dataType = "string",name = "password",value = "密码",required = true),
    })
    public ResultVO<Students> login(String student_id, String password) throws Exception {
        return studentsService.login(student_id,password);
    }

    /**
     * 获取所有学生列表
     */
    @PostMapping("/getStudentList")
    @ApiOperation(value = "获取学生列表")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string", name = "studentId", value = "学生学号", required = false),
            @ApiImplicitParam(dataType = "string", name = "studentName", value = "学生姓名", required = false),
            @ApiImplicitParam(dataType = "string", name = "classId", value = "班级ID", required = false),
            @ApiImplicitParam(dataType = "string", name = "gender", value = "性别（男/女）", required = false),
            @ApiImplicitParam(dataType = "string", name = "phone", value = "联系电话", required = false),
            @ApiImplicitParam(dataType = "string", name = "email", value = "电子邮箱", required = false),
            @ApiImplicitParam(dataType = "integer", name = "age", value = "年龄", required = false),
            @ApiImplicitParam(dataType = "integer", name = "page", value = "页码", required = true, defaultValue = "1"),
            @ApiImplicitParam(dataType = "integer", name = "size", value = "每页大小", required = true, defaultValue = "10")
    })
    public ResultVO<List<Students>> getStudentList(Students student, Integer page, Integer size) {
        System.out.println("接收参数 - student: " + student);
        System.out.println("接收参数 - page: " + page + ", size: " + size);

        // 处理 page 为 null 的情况，设置默认值
        if (page == null || page <= 0) {
            page = 1;
        }

        // 处理 size 为 null 的情况，设置默认值
        if (size == null || size <= 0) {
            size = 10;
        }

        return studentsService.getStudentList(student, page, size);
    }

    /**
     * 根据学号获取学生详情
     */
    @GetMapping("/getStudentDetail")
    @ApiOperation(value = "根据学号获取学生详情")
    @ApiImplicitParam(name = "studentId", value = "学生学号", required = true, dataType = "string")
    public ResultVO<Students> getStudentDetail( String studentId) {
        return studentsService.getStudentDetail(studentId);
    }

    /**
     * 添加学生
     */
    @PostMapping("/addStudent")
    @ApiOperation(value = "添加学生")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string", name = "studentId", value = "学生学号（必须唯一）", required = true),
            @ApiImplicitParam(dataType = "string", name = "studentName", value = "学生姓名", required = true),
            @ApiImplicitParam(dataType = "string", name = "password", value = "登录密码", required = true),
            @ApiImplicitParam(dataType = "string", name = "classId", value = "班级ID", required = true),
            @ApiImplicitParam(dataType = "string", name = "gender", value = "性别（男/女）", required = false),
            @ApiImplicitParam(dataType = "integer", name = "age", value = "年龄", required = false),
            @ApiImplicitParam(dataType = "string", name = "phone", value = "联系电话", required = false),
            @ApiImplicitParam(dataType = "string", name = "email", value = "电子邮箱", required = false)
    })
    public ResultVO<Students> addStudent(Students student) {
        return studentsService.addStudent(student);
    }

    /**
     * 更新学生信息
     */
    @PostMapping("/updateStudent")
    @ApiOperation(value = "更新学生信息")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string", name = "studentId", value = "学生学号（必须存在）", required = true),
            @ApiImplicitParam(dataType = "string", name = "studentName", value = "学生姓名", required = false),
            @ApiImplicitParam(dataType = "string", name = "password", value = "登录密码", required = false),
            @ApiImplicitParam(dataType = "string", name = "classId", value = "班级ID", required = false),
            @ApiImplicitParam(dataType = "string", name = "gender", value = "性别（男/女）", required = false),
            @ApiImplicitParam(dataType = "integer", name = "age", value = "年龄", required = false),
            @ApiImplicitParam(dataType = "string", name = "phone", value = "联系电话", required = false),
            @ApiImplicitParam(dataType = "string", name = "email", value = "电子邮箱", required = false)
    })
    public ResultVO<Students> updateStudent(Students student) {
        return studentsService.updateStudent(student);
    }

    /**
     * 删除学生（根据学号）
     */
    @DeleteMapping("/deleteStudent")
    @ApiOperation(value = "删除学生")
    @ApiImplicitParam(name = "studentId", value = "学生学号", required = true, dataType = "string")
    public ResultVO<String> deleteStudent(String studentId) {
        return studentsService.deleteStudent(studentId);
    }

    /**
     * 根据班级ID查询学生人数
     */
    @GetMapping("/studentCount")
    @ApiOperation(value = "根据班级id查询学生人数")
    @ApiImplicitParam(name = "classId", value = "班级ID", required = true, dataType = "string")
    public ResultVO<Integer> selectStudentCountByClassId(String classId) {
        return studentsService.selectStudentCountByClassId(classId);
    }

    /**
     * 获取所有学生列表
     */
    @PostMapping("/selectStudentListByClassIds")
    @ApiOperation(value = "根据班级ID串获取学生列表")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string", name = "classIds", value = "班级ID串", required = true),
            @ApiImplicitParam(dataType = "integer", name = "page", value = "页码", required = true, defaultValue = "1"),
            @ApiImplicitParam(dataType = "integer", name = "size", value = "每页大小", required = true, defaultValue = "10")
    })
    public ResultVO<List<Students>> selectStudentListByClassIds(String classIds, int page, int size) {
        return studentsService.selectStudentListByClassIds(classIds,page,size);
    }
    /**
     * 组合查询学生个人信息与班级信息
     */
    @GetMapping("/selectStudentAndClassInfo")
    @ApiOperation(value = "组合查询学生个人信息与班级信息")
    @ApiImplicitParam(name = "studentId", value = "学生学号", required = true, dataType = "string")
    public ResultVO<ClassesAndStudentsDTO> selectStudentAndClassInfo(String studentId) {
        return studentsService.selectStudentAndClassInfo(studentId);
    }
}
